﻿using Sidvall.Net;
using Sidvall.UI.Dialogs;
using Sidvall.UI.Messaging;
using Sidvall.UI.ViewModels;
using Sidvall.UI.Wpf.Controls;
using System.Threading.Tasks;

namespace Sidvall.UI.Wpf.Dialogs
{
    public class PingFederateIdentityProviderDialogManager : IIdentityProviderDialogManager
    {
        #region Public Members

        #region CallIdentityProviderAsync

        public async Task<HttpResponseItem> CallIdentityProviderAsync(IHttpClient client, HttpResponseItem responseItem)
        {
            var userName = this.IdentityProviderControl.ViewModel.UserName;
            var password = this.IdentityProviderControl.GetPassword();

            // Post username & password to external authorization server
            var form = "pf.adapterId=" + System.Net.WebUtility.UrlEncode("FormWAMVang")
                + "&pf.ok=clicked&pf.username=" + System.Net.WebUtility.UrlEncode(userName) + "&pf.pass=" + System.Net.WebUtility.UrlEncode(password);
            var response = await client.PostAsync(responseItem.Location, form,
                Sidvall.Net.Mime.MediaType.ApplicationFormUrlEncoded, System.Threading.CancellationToken.None);
            var location = responseItem.Location;
            while (response.StatusCode == System.Net.HttpStatusCode.Found)
            {
                location = response.Headers.Location;
                response = await client.GetAsync(location, HttpCompletionOption.ResponseContentRead, System.Threading.CancellationToken.None);
            }
            var content = await response.GetContentAsStringAsync();
            return new HttpResponseItem()
            {
                Response = response,
                Location = location,
                Content = content,
            };
        }

        #endregion
        #region GetUserControlAsync

        public Task<object> GetUserControlAsync(IIdentityProviderDialogManagerHandler handler)
        {
            this.IdentityProviderDialogManagerHandler = handler;
            if (this.IdentityProviderControl == null)
                this.IdentityProviderControl = new PingFederateIdentityProviderControl();
            this.IdentityProviderControl.ViewModel = new PingFederateIdentityProviderControlViewModel();
            return Task.FromResult((object)this.IdentityProviderControl);
        }

        #endregion
        #region ToString

        public override string ToString()
        {
            return "IdentityProvider - PingFederate";
        }

        #endregion
        #region Unload

        public void Unload()
        {
            this.IdentityProviderControl = null;
            this.IdentityProviderDialogManagerHandler = null;
        }

        #endregion

        #endregion
        #region Private Members

        private IIdentityProviderDialogManagerHandler IdentityProviderDialogManagerHandler { get; set; }

        #region IdentityProviderControl

        private PingFederateIdentityProviderControl _IdentityProviderControl;
        private PingFederateIdentityProviderControl IdentityProviderControl
        {
            get
            {
                return _IdentityProviderControl;
            }
            set
            {
                if (_IdentityProviderControl != null)
                {
                    _IdentityProviderControl.GoBackSelected -= OnGoBackSelected;
                    _IdentityProviderControl.LogOnSelected -= OnLogOnSelected;
                }
                _IdentityProviderControl = value;
                if (_IdentityProviderControl != null)
                {
                    _IdentityProviderControl.GoBackSelected += OnGoBackSelected;
                    _IdentityProviderControl.LogOnSelected += OnLogOnSelected;
                }
            }
        }

        #endregion

        #endregion
        #region Events

        #region OnGoBackSelected

        private void OnGoBackSelected(object sender, System.EventArgs e)
        {
            this.IdentityProviderDialogManagerHandler.GoBack();
        }

        #endregion
        #region OnLogOnSelected

        private void OnLogOnSelected(object sender, System.EventArgs e)
        {
            this.IdentityProviderDialogManagerHandler.LogOn();
        }

        #endregion

        #endregion
        #region Constructors

        public PingFederateIdentityProviderDialogManager()
        {
        }

        #endregion
    }
}
